package com.css;

import com.css.controller.PersonController;
import com.css.entity.CustomShortcutKeyKey;
import com.css.entity.CustomShortcutKeyName;
import com.css.entity.User;
import com.css.handler.UploadHandler;
import com.css.listener.Monitor;
import com.css.listener.MyKeyCode;
import com.css.service.PersonService;
import com.css.util.Function;
import com.css.util.PcUtil;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import lombok.Getter;


import javax.swing.*;
import java.awt.*;
import java.awt.Menu;
import java.awt.MenuItem;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * -
 * </p>
 *
 * @author css
 * @since 2019/1/9
 */
public class Main extends Application {
    static SystemTray tray = SystemTray.getSystemTray();
    private UploadHandler upload = new UploadHandler();
    private static TrayIcon trayIcon = null;
    private static Stage stage;
    private static Monitor monitor = new Monitor();
    private PersonService personService = new PersonService();
    private static List<Function<User>> functions = new ArrayList<>();
    @Getter
    private static User user;
    @Getter
    private static Main main;

    public void init() {
        this.main = this;
        monitor.startOrStop();
        miniTray();
        System.out.println("Main.init");

        try {
            personService.login(PcUtil.getCpuId(), (user) -> {
                Main.user = user;
                initUser(Main.user);
                for (Function<User> function : functions) {
                    function.apply(Main.user);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initUser(User user) {
        List<CustomShortcutKeyName> customShortcutKeyNameList = new ArrayList<>();
        customShortcutKeyNameList.add(createCustomShortcutKeyName("复制", MyKeyCode.CONTROL.vkCode, MyKeyCode.C.vkCode));
        customShortcutKeyNameList.add(createCustomShortcutKeyName("删除", MyKeyCode.CONTROL.vkCode, MyKeyCode.D.vkCode));
        customShortcutKeyNameList.add(createCustomShortcutKeyName("保存", MyKeyCode.CONTROL.vkCode, MyKeyCode.S.vkCode));
        customShortcutKeyNameList.add(createCustomShortcutKeyName("后退", MyKeyCode.CONTROL.vkCode, MyKeyCode.Z.vkCode));
        customShortcutKeyNameList.add(createCustomShortcutKeyName("前进", MyKeyCode.CONTROL.vkCode, MyKeyCode.Y.vkCode));
        customShortcutKeyNameList.add(createCustomShortcutKeyName("格式化", MyKeyCode.CONTROL.vkCode,MyKeyCode.SHIFT.vkCode, MyKeyCode.F.vkCode));
        user.setCustomShortcutKeyNameList(customShortcutKeyNameList);
    }

    private CustomShortcutKeyName createCustomShortcutKeyName(String name, Integer... vkCode) {
        CustomShortcutKeyName customShortcutKeyName = new CustomShortcutKeyName();
        customShortcutKeyName.setKeyName(name);
        List<CustomShortcutKeyKey> customShortcutKeyKeyList = new ArrayList<>();
        for (Integer integer : vkCode) {
            customShortcutKeyKeyList.add(new CustomShortcutKeyKey(integer));
        }
        customShortcutKeyName.setCustomShortcutKeyKeyList(customShortcutKeyKeyList);
        return customShortcutKeyName;
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        System.out.println("Main.start");
        primaryStage.initStyle(StageStyle.UNDECORATED);
        Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("xml/main.fxml"));
        DropShadow dropshadow = new DropShadow();// 阴影向外
        dropshadow.setRadius(50);// 颜色蔓延的距离
        dropshadow.setOffsetX(0);// 水平方向，0则向左右两侧，正则向右，负则向左
        dropshadow.setOffsetY(0);// 垂直方向，0则向上下两侧，正则向下，负则向上
        dropshadow.setSpread(0.1);// 颜色变淡的程度
        dropshadow.setColor(Color.BLACK);// 设置颜色
        Scene scene = new Scene(root);
//        root.setEffect(dropshadow);// 绑定指定窗口控件

        primaryStage.setScene(scene);
        primaryStage.show();
        this.stage = primaryStage;
        upload.start();
    }

    protected static PopupMenu createPopupMenu() {
        final PopupMenu popup = new PopupMenu();
        MenuItem aboutItem = new MenuItem("关于");
        CheckboxMenuItem cb1 = new CheckboxMenuItem("设置1");
        CheckboxMenuItem cb2 = new CheckboxMenuItem("设置2");
        Menu displayMenu = new Menu("测试");
        MenuItem errorItem = new MenuItem("测试4");
        MenuItem warningItem = new MenuItem("测试3");
        MenuItem infoItem = new MenuItem("测试2");
        MenuItem noneItem = new MenuItem("测试1");
        MenuItem exitItem = new MenuItem("退出");
        // Add components to pop-up menu
        popup.add(aboutItem);
        popup.addSeparator();
        popup.add(cb1);
        popup.add(cb2);
        popup.addSeparator();
        popup.add(displayMenu);
        displayMenu.add(errorItem);
        displayMenu.add(warningItem);
        displayMenu.add(infoItem);
        displayMenu.add(noneItem);
        popup.add(exitItem);
        exitItem.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        return popup;
    }

    private static void miniTray() { // 窗口最小化到任务栏托盘

        ImageIcon trayImg = new ImageIcon("monitor.png");// 托盘图标

        trayIcon = new TrayIcon(trayImg.getImage(), "键盘监控", new PopupMenu());
        trayIcon.setImageAutoSize(true);
        // 创建弹出菜单
        final PopupMenu popup = createPopupMenu();
        trayIcon.setPopupMenu(popup);
        trayIcon.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
//                if (e.getClickCount() == 1) {// 单击 1 双击 2
//                    tray.remove(trayIcon);
//                    mf.setVisible(true);
//                    mf.setExtendedState(JFrame.NORMAL);
//                    mf.toFront();
//                }
            }
        });
        try {
            tray.add(trayIcon);
        } catch (AWTException e1) {
            e1.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }

    public static Stage getStage() {
        return stage;
    }

    public static Monitor getMonitor() {
        return monitor;
    }

    public static void getUser(Function<User> function) {
        if (Main.user != null) {
            function.apply(Main.user);
            return;
        }
        functions.add(function);
    }

}
